#include "defs.h"

BOOL __stdcall Cb_IsSecure
(
	PHTTP_FILTER_CONTEXT pFC
)
{
	return pFC->fIsSecurePort;
}

BOOL __stdcall Cb_GetVariable
(
	PHTTP_FILTER_CONTEXT pFC, 
	char * nameBuffer, 
	char * valueBuffer, 
	int * lengthPtr
)
{
	try
	{
		Helper_T::GetServerVariable(pFC, nameBuffer, valueBuffer, lengthPtr);
	}
	catch(Exception_T & exp)
	{
		SetLastError(exp.Code);
		return FALSE;
	}

	return TRUE;
}

BOOL __stdcall Cb_SetHeader
(
	PHTTP_FILTER_CONTEXT pFC, 
	void * pvNotify,
	DWORD dwNotifyType, 
	char * nameBuffer, 
	char * valueBuffer
)
{
	SetHeaderFn_T fn = 0;

	switch( dwNotifyType )
	{
		case SF_NOTIFY_PREPROC_HEADERS:
			fn = ((PHTTP_FILTER_PREPROC_HEADERS)pvNotify)->SetHeader;
			break;

		case SF_NOTIFY_AUTH_COMPLETE:
			fn = ((PHTTP_FILTER_AUTH_COMPLETE_INFO)pvNotify)->SetHeader;
			break;

		case SF_NOTIFY_SEND_RESPONSE:
			fn = ((PHTTP_FILTER_SEND_RESPONSE)pvNotify)->SetHeader;
			break;
	}

	if( !fn )
	{
		return TRUE;
	}

	try
	{
		Helper_T::SetHeader(pFC, fn, nameBuffer, valueBuffer);
	}
	catch(Exception_T & exp)
	{
		SetLastError(exp.Code);
		return FALSE;
	}

	return TRUE;
}

BOOL __stdcall Cb_GetHeader
(
	PHTTP_FILTER_CONTEXT pFC, 
	void * pvNotify,
	DWORD dwNotifyType, 
	char * nameBuffer, 
	char * valueBuffer, 
	int * lengthPtr
)
{
	GetHeaderFn_T fn = 0;

	switch( dwNotifyType )
	{
		case SF_NOTIFY_PREPROC_HEADERS:
			fn = ((PHTTP_FILTER_PREPROC_HEADERS)pvNotify)->GetHeader;
			break;

		case SF_NOTIFY_AUTH_COMPLETE:
			fn = ((PHTTP_FILTER_AUTH_COMPLETE_INFO)pvNotify)->GetHeader;
			break;

		case SF_NOTIFY_SEND_RESPONSE:
			fn = ((PHTTP_FILTER_SEND_RESPONSE)pvNotify)->GetHeader;
			break;
	}

	if( !fn )
	{
		return TRUE;
	}

	try
	{
		Helper_T::GetHeader(pFC, fn, nameBuffer, valueBuffer, lengthPtr);
	}
	catch(Exception_T & exp)
	{
		SetLastError(exp.Code);
		return FALSE;
	}

	return TRUE;
}

void __stdcall Cb_GetUrlMap
(
	PHTTP_FILTER_URL_MAP pUrlMap, 
	char * urlBuffer, 
	int urlLength, 
	char * pathBuffer, 
	int pathLength
)
{
	int urlLengthToCopy = pUrlMap->pszURL ? lstrlenA(pUrlMap->pszURL) : 0;
	int pathLengthToCopy = pUrlMap->cbPathBuff;

	if( urlLengthToCopy && urlLength )
	{
		FillMemory(urlBuffer, urlLength, 0);
		RtlMoveMemory(
			urlBuffer, 
			pUrlMap->pszURL, 
			(urlLength - 1) < urlLengthToCopy ? (urlLength - 1) : urlLengthToCopy);
	}

	if( pathLengthToCopy && pathLength )
	{
		FillMemory(pathBuffer, pathLength, 0);
		RtlMoveMemory(
			pathBuffer, 
			pUrlMap->pszPhysicalPath, 
			(pathLength - 1) < pathLengthToCopy ? (pathLength - 1) : pathLengthToCopy);
	}
}

void __stdcall Cb_SetUrlMap
(
	PHTTP_FILTER_URL_MAP pUrlMap, 
	char * pathBuffer
)
{
	int pathLengthToCopy = pathBuffer ? lstrlenA(pathBuffer) : 0;

	if( pathLengthToCopy )
	{
		FillMemory(pUrlMap->pszPhysicalPath, pUrlMap->cbPathBuff, 0);
		RtlMoveMemory(
			pUrlMap->pszPhysicalPath, 
			pathBuffer, 
			(pUrlMap->cbPathBuff - 1) < (DWORD)pathLengthToCopy ? (pUrlMap->cbPathBuff - 1) : pathLengthToCopy);
	}
}

void __stdcall Cb_GetUserPass
(
	PHTTP_FILTER_AUTHENT pAuthEnt, 
	char * userBuffer, 
	int userLength, 
	char * passBuffer, 
	int passLength
)
{
	int userLengthToCopy = pAuthEnt->cbUserBuff;
	int passLengthToCopy = pAuthEnt->cbPasswordBuff;

	if( userLengthToCopy && userLength )
	{
		FillMemory(userBuffer, userLength, 0);
		RtlMoveMemory(
			userBuffer, 
			pAuthEnt->pszUser, 
			(userLength - 1) < userLengthToCopy ? (userLength - 1) : userLengthToCopy);
	}

	if( passLengthToCopy && passLength )
	{
		FillMemory(passBuffer, passLength, 0);
		RtlMoveMemory(
			passBuffer, 
			pAuthEnt->pszPassword, 
			(passLength - 1) < passLengthToCopy ? (passLength - 1) : passLengthToCopy);
	}
}

void __stdcall Cb_SetUserPass(PHTTP_FILTER_AUTHENT pAuthEnt, char * userBuffer, char * passBuffer)
{
	int userLengthToCopy = userBuffer ? lstrlenA(userBuffer) : 0;
	int passLengthToCopy = passBuffer ? lstrlenA(passBuffer) : 0;

	if( userLengthToCopy )
	{
		FillMemory(pAuthEnt->pszUser, SF_MAX_USERNAME, 0);
		RtlMoveMemory(
			pAuthEnt->pszUser, 
			userBuffer, 
			(SF_MAX_USERNAME - 1) < userLengthToCopy ? (SF_MAX_USERNAME - 1) : userLengthToCopy);
	}

	if( passLengthToCopy )
	{
		FillMemory(pAuthEnt->pszPassword, SF_MAX_PASSWORD, 0);
		RtlMoveMemory(
			pAuthEnt->pszPassword, 
			passBuffer, 
			(SF_MAX_PASSWORD - 1) < passLengthToCopy ? (SF_MAX_PASSWORD - 1) : passLengthToCopy);
	}
}

void __stdcall Cb_GetAccessDenied
(
	PHTTP_FILTER_ACCESS_DENIED pAccessDenied, 
	char * urlBuffer, 
	int urlLength, 
	char * pathBuffer, 
	int pathLength
)
{
	int urlLengthToCopy = pAccessDenied->pszURL ? lstrlenA(pAccessDenied->pszURL) : 0;
	int pathLengthToCopy = pAccessDenied->pszPhysicalPath ? lstrlenA(pAccessDenied->pszPhysicalPath) : 0;

	if( urlLengthToCopy && urlLength )
	{
		FillMemory(urlBuffer, urlLength, 0);
		RtlMoveMemory(
			urlBuffer, 
			pAccessDenied->pszURL, 
			(urlLength - 1) < urlLengthToCopy ? (urlLength - 1) : urlLengthToCopy);
	}

	if( pathLengthToCopy && pathLength )
	{
		FillMemory(pathBuffer, pathLength, 0);
		RtlMoveMemory(
			pathBuffer, 
			pAccessDenied->pszPhysicalPath, 
			(pathLength - 1) < pathLengthToCopy ? (pathLength - 1) : pathLengthToCopy);
	}
}

BOOL __stdcall Cb_WriteClient
(
	PHTTP_FILTER_CONTEXT pFC, 
	unsigned char * bytesBuffer, 
	int bytesLength
)
{
	if( bytesLength )
	{
		return pFC->WriteClient(pFC, bytesBuffer, (DWORD *)&bytesLength, 0);
	}

	return TRUE;
}

void __stdcall Cb_ReadRaw
(
	PHTTP_FILTER_RAW_DATA pRaw,
	unsigned char * rawBuffer, 
	int rawLength
)
{
	if( pRaw->cbInData && rawLength )
	{
		FillMemory(rawBuffer, rawLength, 0);
		RtlMoveMemory(
			rawBuffer, 
			pRaw->pvInData, 
			(DWORD)rawLength < pRaw->cbInData ? rawLength : pRaw->cbInData);
	}
}

BOOL __stdcall Cb_WriteRaw
(
	PHTTP_FILTER_CONTEXT pFC, 
	PHTTP_FILTER_RAW_DATA pRaw,
	unsigned char * rawBuffer, 
	int rawLength
)
{
	if( rawLength )
	{
		pRaw->pvInData = pFC->AllocMem(pFC, rawLength, 0);
		if( !pRaw->pvInData )
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}
		pRaw->cbInBuffer = rawLength;
		pRaw->cbInData = rawLength;
		RtlMoveMemory(pRaw->pvInData, rawBuffer, rawLength);
	}

	return TRUE;
}

void __stdcall Cb_GetLog
(
	PHTTP_FILTER_LOG pLog,
	char * clientHostNameBuffer, 
	int clientHostNameLength,
	char * clientUserNameBuffer, 
	int clientUserNameLength,
	char * serverNameBuffer, 
	int serverNameLength,
	char * operationBuffer, 
	int operationLength,
	char * targetBuffer, 
	int targetLength,
	char * parametersBuffer, 
	int parametersLength
)
{
	int clientHostNameLengthToCopy = pLog->pszClientHostName ? lstrlenA(pLog->pszClientHostName) : 0;
	int clientUserNameLengthToCopy = pLog->pszClientUserName ? lstrlenA(pLog->pszClientUserName) : 0;
	int serverNameLengthToCopy = pLog->pszServerName ? lstrlenA(pLog->pszServerName) : 0;
	int operationLengthToCopy = pLog->pszOperation ? lstrlenA(pLog->pszOperation) : 0;
	int targetLengthToCopy = pLog->pszTarget ? lstrlenA(pLog->pszTarget) : 0;
	int parametersLengthToCopy = pLog->pszParameters ? lstrlenA(pLog->pszParameters) : 0;

	if( clientHostNameLengthToCopy && clientHostNameLength )
	{
		FillMemory(clientHostNameBuffer, clientHostNameLength, 0);
		RtlMoveMemory(
			clientHostNameBuffer, 
			pLog->pszClientHostName, 
			(clientHostNameLength - 1) < clientHostNameLengthToCopy ? (clientHostNameLength - 1) : clientHostNameLengthToCopy);
	}

	if( clientUserNameLengthToCopy && clientUserNameLength )
	{
		FillMemory(clientUserNameBuffer, clientUserNameLength, 0);
		RtlMoveMemory(
			clientUserNameBuffer, 
			pLog->pszClientUserName, 
			(clientUserNameLength - 1) < clientUserNameLengthToCopy ? (clientUserNameLength - 1) : clientUserNameLengthToCopy);
	}

	if( serverNameLengthToCopy && serverNameLength )
	{
		FillMemory(serverNameBuffer, serverNameLength, 0);
		RtlMoveMemory(
			serverNameBuffer, 
			pLog->pszServerName, 
			(serverNameLength - 1) < serverNameLengthToCopy ? (serverNameLength - 1) : serverNameLengthToCopy);
	}

	if( operationLengthToCopy && operationLength )
	{
		FillMemory(operationBuffer, operationLength, 0);
		RtlMoveMemory(
			operationBuffer, 
			pLog->pszOperation, 
			(operationLength - 1) < operationLengthToCopy ? (operationLength - 1) : operationLengthToCopy);
	}

	if( targetLengthToCopy && targetLength )
	{
		FillMemory(targetBuffer, targetLength, 0);
		RtlMoveMemory(
			targetBuffer, 
			pLog->pszTarget, 
			(targetLength - 1) < targetLengthToCopy ? (targetLength - 1) : targetLengthToCopy);
	}

	if( parametersLengthToCopy && parametersLength )
	{
		FillMemory(parametersBuffer, parametersLength, 0);
		RtlMoveMemory(
			parametersBuffer, 
			pLog->pszParameters, 
			(parametersLength - 1) < parametersLengthToCopy ? (parametersLength - 1) : parametersLengthToCopy);
	}
}

bool __stdcall Cb_SetLog
(
	PHTTP_FILTER_CONTEXT pFC,
	PHTTP_FILTER_LOG pLog,
	char * clientHostNameBuffer,
	char * clientUserNameBuffer,
	char * serverNameBuffer,
	char * operationBuffer,
	char * targetBuffer,
	char * parametersBuffer,
	int dwHttpStatus,
	int dwWin32Status,
	int dwBytesSent,
	int dwBytesRecvd,
	int msTimeForProcessing
)
{
	int clientHostNameLengthToCopy = clientHostNameBuffer ? lstrlenA(clientHostNameBuffer) : 0;
	int clientUserNameLengthToCopy = clientUserNameBuffer ? lstrlenA(clientUserNameBuffer) : 0;
	int serverNameLengthToCopy = serverNameBuffer ? lstrlenA(serverNameBuffer) : 0;
	int operationLengthToCopy = operationBuffer ? lstrlenA(operationBuffer) : 0;
	int targetLengthToCopy = targetBuffer ? lstrlenA(targetBuffer) : 0;
	int parametersLengthToCopy = parametersBuffer ? lstrlenA(parametersBuffer) : 0;

	if( clientHostNameLengthToCopy )
	{
		pLog->pszClientHostName = (char *)pFC->AllocMem(pFC, clientHostNameLengthToCopy + 1, 0);
		if( !pLog->pszClientHostName )
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}
		RtlMoveMemory((char *)pLog->pszClientHostName, clientHostNameBuffer, clientHostNameLengthToCopy);
		((char *)pLog->pszClientHostName)[clientHostNameLengthToCopy] = '\0';
	}

	if( clientUserNameLengthToCopy )
	{
		pLog->pszClientUserName = (char *)pFC->AllocMem(pFC, clientUserNameLengthToCopy + 1, 0);
		if( !pLog->pszClientUserName )
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}
		RtlMoveMemory((char *)pLog->pszClientUserName, clientUserNameBuffer, clientUserNameLengthToCopy);
		((char *)pLog->pszClientUserName)[clientUserNameLengthToCopy] = '\0';
	}

	if( serverNameLengthToCopy )
	{
		pLog->pszServerName = (char *)pFC->AllocMem(pFC, serverNameLengthToCopy + 1, 0);
		if( !pLog->pszServerName )
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}
		RtlMoveMemory((char *)pLog->pszServerName, serverNameBuffer, serverNameLengthToCopy);
		((char *)pLog->pszServerName)[serverNameLengthToCopy] = '\0';
	}

	if( operationLengthToCopy )
	{
		pLog->pszOperation = (char *)pFC->AllocMem(pFC, operationLengthToCopy + 1, 0);
		if( !pLog->pszOperation )
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}
		RtlMoveMemory((char *)pLog->pszOperation, operationBuffer, operationLengthToCopy);
		((char *)pLog->pszOperation)[operationLengthToCopy] = '\0';
	}

	if( targetLengthToCopy )
	{
		pLog->pszTarget = (char *)pFC->AllocMem(pFC, targetLengthToCopy + 1, 0);
		if( !pLog->pszTarget )
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}
		RtlMoveMemory((char *)pLog->pszTarget, targetBuffer, targetLengthToCopy);
		((char *)pLog->pszTarget)[targetLengthToCopy] = '\0';
	}

	if( parametersLengthToCopy )
	{
		pLog->pszParameters = (char *)pFC->AllocMem(pFC, parametersLengthToCopy + 1, 0);
		if( !pLog->pszParameters )
		{
			SetLastError(ERROR_NOT_ENOUGH_MEMORY);
			return FALSE;
		}
		RtlMoveMemory((char *)pLog->pszParameters, parametersBuffer, parametersLengthToCopy);
		((char *)pLog->pszParameters)[parametersLengthToCopy] = '\0';
	}

	pLog->dwHttpStatus = dwHttpStatus;
	pLog->dwWin32Status = dwWin32Status;
	pLog->dwBytesSent = dwBytesSent;
	pLog->dwBytesRecvd = dwBytesRecvd;
	pLog->msTimeForProcessing = msTimeForProcessing;

	return TRUE;
}